<?php
//set path alias
Yii::setPathOfAlias('common', DIR_COMMON);
Yii::setPathOfAlias('frontend', DIR_FRONTEND.'protected');
Yii::setPathOfAlias('backend', DIR_BACKEND.'protected');

//get url base
function getUrl(){
    $baseUrl = Yii::app()->baseUrl;
    if (strpos($baseUrl,'backend') === false) {
        return $baseUrl."/frontend/";
    }
    return $baseUrl."/";
}

//add file css
function addCss($file,$is_common = false){
    
    $cs = Yii::app()->getClientScript();
    if($is_common)
        $cs->registerCssFile(BASE_URL.'/common/www/css/'.$file);
    else 
        $cs->registerCssFile(getUrl().'www/css/'.$file);

}
//add file js
function addJs($file,$is_common = false){
    $baseUrl = Yii::app()->baseUrl;
    $cs = Yii::app()->getClientScript();
    if($is_common)
        $cs->registerScriptFile(BASE_URL.'/common/www/js/'.$file);
    else 
        $cs->registerScriptFile(getUrl().'www/js/'.$file);

}
//url css directory
function urlCss(){
    return getUrl()."www/css/";
}
//url js directory
function urlJs(){
    return getUrl()."www/js/";
}

function loopChild($categories,$break,$id,&$return,$startChar,$char){
    if($break != null && $break == $id){
        return false;
    }else{
        
        $return[$id] = $startChar." ".$categories[$id]['data']->name;
        $startChar = $startChar.$char;
        foreach($categories[$id]['children'] as $value){
               
                loopChild($categories,$break,$value,$return,$startChar,$char);
        }
    }
    
}

/**
 * get list category for dropdown
 * @param type $char
 * @return array 
 */
function getlistCategory($break = null,$char = "-"){
    $categories = Category::getTreeCategory();
    $return = array();
    //echo "<pre>";var_dump($categories);die();
    loopChild($categories,$break,0,$return,'',$char);
    
    return $return;
}